<template>
  <div class="container">
    <el-card>
      <!-- 表头数据 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="学科名称">
              <el-input
                style="200px"
                v-model="requestParams.subjectName"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clear">清除</el-button>
              <el-button type="primary" size="small" @click="filter"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="AddSubject()"
            >新增学科</el-button
          >
        </el-col>
      </el-row>
      <!--消息提示  -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 数据表格 -->
      <el-table :data="SubjectsList" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="subjectName" label="学科名称"></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template #default="{ row }">{{
            row.addDate | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template #default="{ row }">{{
            row.isFrontDisplay === 1 ? '是' : '否'
          }}</template>
        </el-table-column>
        <el-table-column
          prop="twoLevelDirectory"
          label="二级目录"
        ></el-table-column>
        <el-table-column prop="tags" label="标签"></el-table-column>
        <el-table-column prop="totals" label="题目数量"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template #default="{ row }">
            <!-- <p>{{row}}</p> -->
            <el-button
              type="text"
              @click="
                $router.push(`directorys?id=${row.id}&name=${row.subjectName}`)
              "
              >学科分类</el-button
            >
            <el-button
              type="text"
              @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)"
              >学科标签</el-button
            >
            <el-button type="text" @click="AddSubject(row)">修改</el-button>
            <el-button type="text" @click="delSubject(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        :page-sizes="[5, 10, 15, 20]"
        :page-size="requestParams.pagesize"
        layout="prev, pager, next, sizes, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加或修改的对话框 -->
    <!-- <subjects-add ref="subjectsadd" :data="currSubject"></subjects-add> -->
    <SubjectsAdd
      ref="subjectsadd"
      :data="currSubject"
      @updateList="updateList()"
    ></SubjectsAdd>
  </div>
</template>

<script>
import { list as getSubjects, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'SubjectsList',
  components: { SubjectsAdd },
  data () {
    return {
      requestParams: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      // 学科列表
      SubjectsList: [],
      total: 0,
      // 当前学科
      currSubject: {},
      currentPage: 2

    }
  },
  created () {
    this.getSubjectList()
  },
  methods: {
    // 获取列表数据
    async getSubjectList () {
      const { data } = await getSubjects(this.requestParams)
      console.log(data, 3)
      this.SubjectsList = data.items
      this.total = data.counts
    },
    // 清除输入框的内容
    clear () {
      this.requestParams = {
        subjectName: '',
        page: 1,
        pagesize: 10
      }
    },
    // 搜索
    filter () {
      this.requestParams.page = 1
      this.getSubjectList()
    },
    handleSizeChange (val) {
      this.requestParams.page = 1
      this.requestParams.pagesize = val
      this.getSubjectList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.requestParams.page = val
      this.getSubjectList()
      console.log(`当前页: ${val}`)
    },
    // 更新每一页的序号索引值
    indexMethod (index) {
      // console.log(index, 5)
      return index + 1 + this.requestParams.pagesize * (this.requestParams.page - 1)
    },
    // 添加学科
    AddSubject (subject) {
      this.currSubject = subject || {}
      // console.log(subject, 99)
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    // 删除学科
    async delSubject (subject) {
      console.log('点击了', subject)
      // 删除是高危操作，需要弹框提醒
      await this.$confirm('此操作不可逆，确定删除?', '温馨提示', {
        confirmButtonText: '确定',
        cancelBUttonText: '取消',
        type: 'warning'
      })
      await remove(subject)
      this.$message.success('删除学科成功')
      this.getSubjectList()
    },
    // 修改或新增后的列表更新
    updateList () {
      this.getSubjectList()
    }
  }
}
</script>

<style scoped lang='less'></style>
